/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import tpa.TablaPeriodica.SubClase;

/**
 *
 * @author leandro
 */
public class AgregarSubClaseAction extends TablaPeriodicaBaseAction {

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        try {
            System.out.println("Agregando SubClase");
            AgregarSubClaseActionForm actionForm = (AgregarSubClaseActionForm)form;
            actionForm.setStatus("");

            SubClase subclase = this.getTablaPeriodicaSessionBean().AgregarSubClase(actionForm.getSubClaseId(), actionForm.getValor(), actionForm.getClasificacionId(), actionForm.getElementosElegidos() );
            
            if(actionForm.getSubClaseId() <= 0L){
                actionForm.setStatus("SubClase " + subclase.toString() + " agregada.");
                System.out.println("SubClase " + subclase.toString() + " agregada.");
            }
            else {
                actionForm.setStatus("SubClase " + subclase.toString() + " actualizada.");
                System.out.println("SubClase " + subclase.toString() + " actualizada.");
            }
            
            actionForm.setSubClaseId(subclase.getId());
            
            return mapping.findForward(SUCCESS);
        }
        catch (Exception exception)
        {
            ActionErrors errors = new ActionErrors();

            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.exception", exception.getMessage()));

            saveErrors(request, errors);
            return mapping.findForward(ERROR);
        }
    }
}
